package com.aw.baselib.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.core.content.FileProvider
import com.aw.baselib.util.AwFileUtil.getMIMEType
import java.io.File

/**
 * @author : hzw
 * @date : 2020/4/4
 */
object AwThirdAppJudgeUtil {
    /**
     * 检测是否安装支付宝
     * @param context
     * @return
     */
    fun isAliPayInstalled(context: Context): Boolean {
        val uri = Uri.parse("alipays://platformapi/startApp")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        val componentName = intent.resolveActivity(context.packageManager)
        return componentName != null
    }

    /**
     * 判断微信是否可用
     * @param context
     * @return
     */
    fun isWeixinInstall(context: Context): Boolean {
        val packageManager = context.packageManager // 获取packagemanager
        val pinfo = packageManager.getInstalledPackages(0) // 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (i in pinfo.indices) {
                val pn = pinfo[i].packageName
                if (pn == "com.tencent.mm") {
                    return true
                }
            }
        }
        return false
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    fun isQQClientInstall(context: Context): Boolean {
        val packageManager = context.packageManager
        val pinfo = packageManager.getInstalledPackages(0)
        if (pinfo != null) {
            for (i in pinfo.indices) {
                val pn = pinfo[i].packageName
                if (pn == "com.tencent.mobileqq") {
                    return true
                }
            }
        }
        return false
    }

    fun toThirdApp(context: Context, filePath: String?): Boolean {
        return try {
            val intent = Intent()
            val file = File(filePath)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK //设置标记
            //        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标记
            intent.action = Intent.ACTION_VIEW //动作，查看
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                val uri = FileProvider.getUriForFile(
                    context,
                    context.packageName + ".base.fileProvider",
                    file
                )
                intent.setDataAndType(uri, getMIMEType(file))
            } else {
                intent.setDataAndType(Uri.fromFile(file), getMIMEType(file))
            }
            //        intent.setDataAndType(Uri.fromFile(file), AwFileUtil.getMIMEType(file));//设置类型
            context.startActivity(intent)
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

    fun toBleSetting(activity: Activity) {
        activity.startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS)) //直接跳转到设置界面
    }

    fun toAppSysSetting(context: Context) {
        val intent = Intent()
        intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
        val uri = Uri.fromParts("package", context.packageName, null)
        intent.data = uri
        context.startActivity(intent)
    }

    fun toSysBrowser(context: Context, url: String?) {
        val intent = Intent()
        intent.action = "android.intent.action.VIEW"
        val content_url = Uri.parse(url)
        intent.data = content_url
        context.startActivity(intent)
    }
}